package com.cloudkinto.service.user.captchamode.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.cloudkinto.common.common.CodeGenerator;
import com.cloudkinto.common.common.SmsUtils;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.exception.BizExceptionI18;
import com.cloudkinto.entity.UserDo;
import com.cloudkinto.service.user.captchamode.CaptchaModeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author zhangyy
 * @date 2020/10/15 10:52
 * @description:
 */
@Slf4j
public class PhoneSendMsgImpl implements CaptchaModeService {
    private UserDo userDo;

    private RedisTemplate redisTemplate;

    public PhoneSendMsgImpl(UserDo userDo, RedisTemplate redisTemplate) {
        this.userDo = userDo;
        this.redisTemplate = redisTemplate;
    }


    @Override
    public void sendMsg(int type) {
        String userPhone = userDo.getUserPhone();
        if (StringUtils.isBlank(userPhone)) {
            throw new BizExceptionI18(SysConstant.Phone_NoPhone);
        }
        String code = (String) redisTemplate.opsForValue().get("phoneCodeIsReSend:" + userPhone);
        if (StringUtils.isNotBlank(code)) {
            throw new BizExceptionI18(SysConstant.Phone_SendCode_Min);
        }
        try {
          //  String generate4Number = CodeGenerator.generate4Number();
           String generateCode= (String) redisTemplate.opsForValue().get("phoneCode:" + userDo.getUserPhone());
            SendSmsResponse sendSmsResponse = SmsUtils.sendSms(userPhone, generateCode, type);
            if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                log.debug(userPhone + "------短息发送成功");
            } else if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.MOBILE_NUMBER_ILLEGAL")) {
                throw new BizExceptionI18(SysConstant.Isv_MOBILE_NUMBER_ILLEGAL, userPhone);
            } else if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.MOBILE_COUNT_OVER_LIMIT")) {
                throw new BizExceptionI18(SysConstant.Isv_MOBILE_COUNT_OVER_LIMIT, userPhone);
            } else if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.AMOUNT_NOT_ENOUGH")) {
                throw new BizExceptionI18(SysConstant.Isv_AMOUNT_NOT_ENOUGH, userPhone);
            } else if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.SMS_SIGNATURE_ILLEGAL")) {
                throw new BizExceptionI18(SysConstant.Isv_SMS_SIGNATURE_ILLEGAL, userPhone);
            } else if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("isv.OUT_OF_SERVICE")) {
                throw new BizExceptionI18(SysConstant.Isv_OUT_OF_SERVICE, userPhone);
            } else {
                throw new BizException(userPhone + "：" + sendSmsResponse.getMessage());
            }
            redisTemplate.opsForValue().set("phoneCode:" + userDo.getUserAccount(), generateCode, 5, TimeUnit.MINUTES);
            redisTemplate.opsForValue().set("phoneCodeIsReSend:" + userPhone, userPhone, 60, TimeUnit.SECONDS);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new BizExceptionI18(SysConstant.Phone_Send_Error);
        }
    }


}
